﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Lib.Assembly;
using InnovTech.Persistence;
using NHibernate.Cfg;
using NHibernate.Util;

namespace NHibernate.MsSqlCe
{
    public class NHMsSqlCeDatabaseTester : NHMsSqlCeVirtualEnvironment
    {

        public string DatabasePath { get; private set; }

        public NHMsSqlCeDatabaseTester(NHMsSqlCeDatabaseInfo dbInfo)
        {
            _sessionFactoryBuilder = new MsSqlCeSessionFactoryBuilder(dbInfo);

            DatabasePath = dbInfo.DatabasePath;
        }

       

        public int GetCount<T>()
        {
            int count = 0;
            DoSessionUnitWork(dao =>
            {
                count = dao.List<T>(obj => true).Count();
            });
            return count;
        }

        public void Save(params object[] saveList)
        {
            DoSessionUnitWork(dao =>
            {
                dao.Save(saveList);
            });
        }

        public void Delete(params object[] saveList)
        {
            DoSessionUnitWork(dao =>
            {
                dao.Delete(saveList);
            });
        }

        public T Get<T>(object id)
        {
            T obj = default(T);
            DoSessionUnitWork(dao =>
            {
                obj = dao.Get<T>(id);
            });
            return obj;
        }

        public List<T> List<T>(Func<T, bool> predicate)
        {
            List<T> result = new List<T>();
            DoSessionUnitWork(dao =>
            {
                result = dao.List<T>(predicate).ToList();
            });
            return result;
        }

    }
}
